package com.example.wanbaqian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.ApplyAgainAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.ApplyTwoBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.DesUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.moxie.client.MainActivity;
import com.moxie.client.model.MxParam;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 再次申请
 */
public class ApplyAgainActivity extends BaseActivity implements HttpCallback {
    @BindView(R.id.aa_RecyclerView)
    RecyclerView aaRecyclerView;
    private ApplyTwoBean itemBean;
    private List<ApplyTwoBean> list = new ArrayList<>();
    private ApplyAgainAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_apply_again;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.main_color);
        setToolBar("再次申请");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getDate();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //获取再次申请填充
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_AGNINFO, mContext,
                new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)}), mContext, null, CommonValue.REQUEST_CODE1, this);
    }

    private void getDate() {
        itemBean = new ApplyTwoBean(R.drawable.people_mobile, "手机运营商", "手机运营商有助加快审核", "0");
        list.add(itemBean);
        itemBean = new ApplyTwoBean(R.drawable.alipay_zm, "芝麻信用", "阿里体系的信用查询", "0");
        list.add(itemBean);
//        itemBean = new ApplyTwoBean(R.drawable.taobao_zx, "淘宝授权认证", "淘宝订单账号", "0");
//        list.add(itemBean);
//        itemBean = new ApplyTwoBean(R.drawable.tencen_zx, "QQ授权", "常用的社交软件联系方式", "0");
//        list.add(itemBean);
        aaRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ApplyAgainAdapter(mContext, list);
        aaRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                MxParam mxParam = new MxParam();
                mxParam.setUserId(SPUtils.getUserInfo(mContext).getUser_id() + "");
//                mxParam.setApiKey(CommonValue.TDAUTHKEY);
                //2017年9月8日10:26:19魔蝎apikey修改为我们自己的
                mxParam.setApiKey(CommonValue.MX_API_KEY);
                mxParam.setBannerBgColor("#04C9D4"); // SDK里Banner的背景色
                mxParam.setBannerTxtColor("#FFFFFF"); // SDK里Banner的字体颜色
                Bundle bundle = new Bundle();
                switch (position) {
                    case 0:

                        String stat = list.get(0).getStat();

                        if("0".equals(stat)){
                            //运营商
                            mxParam.setFunction(MxParam.PARAM_FUNCTION_CARRIER);
                            bundle.putParcelable("param", mxParam);
                            openActivityForResult(MainActivity.class, bundle, CommonValue.REQUEST_CODE1);
                        }else{
                            showToast("您已完善运营商信息");
                        }

                        break;
                    case 1://芝麻 //芝麻信用
                        String stat1 = list.get(1).getStat();
                        if("0".equals(stat1)){
                            HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.THIRD_ZHIMA, mContext,
                                    new String[]{"accessid"},
                                    new String[]{SPUtils.getAccessid(mContext)}),
                                    mContext,
                                    null,
                                    CommonValue.REQUEST_CODE5,
                                    ApplyAgainActivity.this);
                        }else{
                            showToast("您已完善芝麻信用");
                        }



                        break;
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case CommonValue.REQUEST_CODE1:
//                case CommonValue.REQUEST_CODE2:
//                case CommonValue.REQUEST_CODE3:
                    String result = data.getExtras().getString("result");
                    if (JsonUtils.getInstance().getInt(result, "code") == 1) {
                        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.THIRD_RESPONSE, mContext,
                                new String[]{"accessid", "response_type", "response_info"},
                                new String[]{SPUtils.getAccessid(mContext),
                                                CommonValue.mobile, result}),
                                "", CommonValue.REQUEST_CODE3, this);
                    } else {
                        showToast("数据获取失败");
                    }
                    break;

            }
        }
    }

    @Override
    protected void initDate() {

    }

    @OnClick(R.id.aa_okBtn)
    public void onClick() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_AGAIN, mContext,
                new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)}), mContext, null, CommonValue.REQUEST_CODE4, this);
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                list.get(0).setStat(JsonUtils.getInstance().getJsonObject(content, "mobile_fill_flag"));
                list.get(1).setStat(JsonUtils.getInstance().getJsonObject(content, "zhima_fill_flag"));
                adapter.notifyDataSetChanged();
                break;
            case CommonValue.REQUEST_CODE4:
                openActivity(QuotaAuditActivity.class);
                finish();
                break;
            case CommonValue.REQUEST_CODE5:
                String decrypt = DesUtils.decrypt(content, SPUtils.getDesKey(mContext));
                Log.e("TAG", "芝麻芝麻onSuccess: "+decrypt);
                Bundle bundle = new Bundle();
                bundle.putString("title", "芝麻信用");
                bundle.putString("url", decrypt);
                openActivity(WebViewActivity.class, bundle);

                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }
}
